﻿using System;
using System.Windows.Forms;
using System.Xml;

namespace TextEditor
{
    

    public class A_OpenDBFile : A_Action
    {
        public override string ActionName()
        {
            return "opendbfile";
        }

        public override bool Execute()
        {
            ET_Document myRecord = new ET_Document();
            base.myOwnerDocument.StopScript();
            base.myOwnerDocument.ClearContent();
            if (myRecord.SetKeyWord(base.Param1) && base.myOwnerDocument.DataSource.DBConn.ReadOneRecord(myRecord))
            {
                base.myOwnerDocument.BeginUpdate();
                XmlDocument dataXML = myRecord.GetDataXML();
                base.myOwnerDocument.FromXML(dataXML.DocumentElement);
                base.myOwnerDocument.Info.ID = myRecord.ObjectID.ToString();
                base.myOwnerDocument.Info.Title = myRecord.ObjectName;
                base.myOwnerDocument.Content.MoveSelectStart(0);
                if (base.myOwnerDocument.OwnerControl != null)
                {
                    base.myOwnerDocument.OwnerControl.HideSmartTag();
                }
                base.myOwnerDocument.ContentChanged();
                base.myOwnerDocument.Modified = false;
                base.myOwnerDocument.EndUpdate();
                base.myOwnerDocument.Refresh();
                return true;
            }
            return false;
        }

        public override bool isEnable()
        {
            return true;
        }

        public override bool UIExecute()
        {
            //using (frmDBFileList list = new frmDBFileList())
            //{
            //    list.DBConn = base.myOwnerDocument.DataSource.DBConn;
            //    list.RefreshList();
            //    if (list.ShowDialog() == DialogResult.OK)
            //    {
            //        ET_Document selectedRecord = list.SelectedRecord;
            //        if (base.myOwnerDocument.DataSource.DBConn.ReadOneRecord(selectedRecord))
            //        {
            //            try
            //            {
            //                XmlDocument dataXML = selectedRecord.GetDataXML();
            //                base.myOwnerDocument.BeginUpdate();
            //                base.myOwnerDocument.FromXML(dataXML.DocumentElement);
            //                base.myOwnerDocument.Info.ID = selectedRecord.ObjectID.ToString();
            //                base.myOwnerDocument.Info.Title = selectedRecord.ObjectName;
            //                base.myOwnerDocument.ContentChanged();
            //                base.myOwnerDocument.Modified = false;
            //                base.myOwnerDocument.EndUpdate();
            //                base.myOwnerDocument.Refresh();
            //                return true;
            //            }
            //            catch (Exception exception)
            //            {
            //                MessageBox.Show("错误的文件格式\r" + exception.ToString());
            //            }
            //        }
            //    }
            //}
            return false;
        }
    }
}

